package com.zyong.design.pattern.command.case2;

/**
 * 调用者
 *
 * 0 电灯
 * 1 电视
 * 2 冰箱
 * 3 洗衣机
 * 4 晾衣架
 *
 * @program: zyong-design-pattern
 * @ClassName: RemoteController
 * @author: zyong
 * @create: 2024-03-27 22:16
 */
public class RemoteController {
    /**
     * 开 按钮的命令数组
     */
    Command[] onCommands;
    /**
     * 关 按钮的命令数组
     */
    Command[] offCommands;

    /**
     * 执行撤销的命令
     */
    Command undoCommand;

    /**
     * 构造函数
     */
    public RemoteController() {
        onCommands = new Command[6];
        offCommands = new Command[6];

        for (int i = 0; i < 6; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    public void setCommand(int no, Command onCommand, Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    /**
     * 按下 开 按钮
     * @param no 数组下标，从0开始
     */
    public void onButtonWasPushed(int no) {
        // 找到对应开的按钮，执行开的命令
        onCommands[no].execute();
        // 记录操作的命令，用于撤销
        undoCommand = onCommands[no];
    }

    /**
     * 按下 关 按钮
     * @param no 数组下标，从0开始
     */
    public void offButtonWasPushed(int no) {
        // 找到对应关的按钮，执行关的命令
        offCommands[no].execute();
        // 记录操作的命令，用于撤销
        undoCommand = offCommands[no];
    }

    /**
     * 按下 撤销 按钮
     */
    public void undoButtonWasPushed() {
        undoCommand.undo();
    }
}
